package com.tinygone.order.service.impl;

import com.tinygone.order.dao.OrderDetailRepository;
import com.tinygone.order.dao.OrderMasterRepository;
import com.tinygone.order.dto.OrderDTO;
import com.tinygone.order.entity.OrderMaster;
import com.tinygone.order.eunm.OrderStatusEnum;
import com.tinygone.order.eunm.PayStatusEnum;
import com.tinygone.order.service.OrderService;
import com.tinygone.order.util.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author liqigang
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        OrderMaster orderMaster = new OrderMaster();

        orderDTO.setOrderId(KeyUtil.generateUniqueKey());
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(new BigDecimal(5));
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        Date createTime = new Date();
        orderMaster.setCreateTime(createTime);
        orderMaster.setUpdateTime(createTime);
        orderMasterRepository.save(orderMaster);
        return orderDTO;
    }
}
